package handler;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
//import java.io.*;
//import java.util.*;
import structures.Image;
import structures.Tag;

/**
 * ImageHandler
 * @author Jose Antonio Prieto
 */
public class ImageHandler extends DefaultHandler {

    /** Image */
    private Image image;
    /** Accumulator */
    private StringBuffer accumulator;

    /**
     * Constructor
     */
    public ImageHandler() {
        image = new Image();
    }

    /**
     * Constructor
     * @param ph
     */
    public ImageHandler(Image ph) {
        image = ph;
    }

    /**
     * Called at the beginning
     */
    public void startDocument() {
        accumulator = new StringBuffer();
    }

    /**
     * When the parser encounters plain text (not XML elements), it calls this method, which accumulates 
     * the text in a string buffer. Note that this method may be called in multiple times.
     * @param buffer
     * @param start
     * @param length
     */
    public void characters(char[] buffer, int start, int length) {
        accumulator.append(buffer, start, length);
    }

    /**
     * At the beginning of each new element, erase any accumulated text and capture attributes
     * @param namespaceURL
     * @param localName
     * @param qname
     * @param attributes
     */
    public void startElement(String namespaceURL, String localName, String qname, Attributes attributes) {
        accumulator.setLength(0);
        if (localName.equals("image")) {
            image.setId(attributes.getValue("id"));
            image.setOwner(attributes.getValue("owner"));
        } else if (localName.equals("tag")) {
            Tag t = new Tag();
            image.addTag(t);
            if (attributes.getLength() > 0) {
                image.getLastTag().setTaxonomy(attributes.getValue("taxonomy"));
            }
        }
    }

    /**
     * Take special action when reach the end of selected elements. 
     * In this case the accumulated text is stored in the image object
     * @param namespaceURL
     * @param localName
     * @param qname
     */
    public void endElement(String namespaceURL, String localName, String qname) {
        
        if (localName.equals("title")) {
            image.setTitle(accumulator.toString().trim());
        } else if (localName.equals("id")) {
            image.setId(accumulator.toString().trim());
        } else if (localName.equals("place")) {
            image.setPlace(accumulator.toString().trim());
        } else if (localName.equals("tagname")) {
            image.getLastTag().setTagname(accumulator.toString().trim());
        } else if (localName.equals("URL")){ 
        	image.setURL(accumulator.toString().trim());
        }else if (localName.equals("initCoordinateX")) {
            image.getLastTag().setInitCoordinateX(Integer.parseInt(accumulator.toString().trim()));
        } else if (localName.equals("endCoordinateX")) {
            image.getLastTag().setEndCoordinateX(Integer.parseInt(accumulator.toString().trim()));
        } else if (localName.equals("initCoordinateY")) {
            image.getLastTag().setInitCoordinateY(Integer.parseInt(accumulator.toString().trim()));
        } else if (localName.equals("endCoordinateY")) {
            image.getLastTag().setEndCoordinateY(Integer.parseInt(accumulator.toString().trim()));
        } else if (localName.equals("interest")) {
            image.setInterest(accumulator.toString().trim());
        } else if (localName.equals("quality")) {
            image.setQuality(accumulator.toString().trim());
        } else if (localName.equals("global")) {
            image.setGlobal(accumulator.toString().trim());
        } else if (localName.equals("make")) {
            image.setMake(accumulator.toString().trim());
        } else if (localName.equals("model")) {
            image.setModel(accumulator.toString().trim());
        } else if (localName.equals("software")) {
            image.setSoftware(accumulator.toString().trim());
        } else if (localName.equals("bitsPerSample")) {
            image.setBitsPerSample(accumulator.toString().trim());
        } else if (localName.equals("compression")) {
            image.setCompression(accumulator.toString().trim());
        } else if (localName.equals("imageDescription")) {
            image.setImageDescription(accumulator.toString().trim());
        } else if (localName.equals("dateTime")) {
            image.setDateTime(accumulator.toString().trim());
        } else if (localName.equals("artist")) {
            image.setArtist(accumulator.toString().trim());
        } else if (localName.equals("copyright")) {
            image.setCopyright(accumulator.toString().trim());
        } else if (localName.equals("flash")) {
            image.setFlash(accumulator.toString().trim());
        } else if (localName.equals("userComment")) {
            image.setUserComment(accumulator.toString().trim());
        } else if (localName.equals("nextID")){
        	image.setNextID(accumulator.toString().trim());
        }
    }

    /**
     * Called at the end
     */
    public void endDocument() {
    }

    /**
     * Warning
     * @param exception
     */
    public void warning(SAXParseException exception) {
        System.err.println("WARNING: line " + exception.getLineNumber() + ": " +
                exception.getMessage());
    }

    /**
     * Error
     * @param exception
     */
    public void error(SAXParseException exception) {
        System.err.println("ERROR: line " + exception.getLineNumber() + ": " +
                exception.getMessage());
    }

    /**
     * Fatal error
     * @param exception
     * @throws org.xml.sax.SAXException
     */
    public void fatalError(SAXParseException exception) throws SAXException {
        System.err.println("FATAL: line " + exception.getLineNumber() + ": " +
                exception.getMessage());
        throw (exception);
    }
}
